package com.sunyard.pcc.web.controller;


import com.sunyard.pcc.common.bo.SysUserBO;
import com.sunyard.pcc.common.constant.ErrorCodeEnum;
import com.sunyard.pcc.common.exception.PccCommonException;
import com.sunyard.pcc.common.shiro.ShiroUtils;
import com.sunyard.pcc.common.validator.ValidationUtils;
import com.sunyard.pcc.core.annotation.SysLog;
import com.sunyard.pcc.core.service.UserService;
import com.sunyard.pcc.web.form.SysUserForm;
import com.sunyard.pcc.web.form.UserForm;
import com.sunyard.michiru.common.entity.BaseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @Author: Hewei
 * @Description: 登录功能对应控制器
 * @Date: Created in 11:51 2018/2/12
 */
@Slf4j
@RestController
@RequestMapping(value = "sys")
public class SysController {

    @Autowired
    private UserService userService;


    @ApiOperation(value = "用户登录")
    @ApiImplicitParam(name = "userForm", value = "用户数据", type = "body", dataType = "UserForm")
    @RequestMapping(value = "/login" , method =RequestMethod.POST)
    public BaseResult<String> login(@RequestBody  UserForm userForm) {

        ValidationUtils.isBlank(userForm.getTelephone(), userForm.getPassword());
        try{
            Subject subject = ShiroUtils.getSubject();
            UsernamePasswordToken token = new UsernamePasswordToken(userForm.getTelephone(), userForm.getPassword());
            subject.login(token);
        } catch (UnknownAccountException e) {
            log.info("", e);
            // 用户不存在
            throw new PccCommonException(ErrorCodeEnum.USER_DO_NOT_EXITS);
        } catch (DisabledAccountException e){
            log.info("", e);
            // 用户被禁用
            throw new PccCommonException(ErrorCodeEnum.USER_IS_DISABLED);
        } catch (IncorrectCredentialsException e){
            log.info("", e);
            // 密码错误
            throw new PccCommonException(ErrorCodeEnum.USER_PASSWORD_NOT_FIXED);
        } catch (ExcessiveAttemptsException e){
            log.info("", e);
            // 错误登录次数限制
            throw new PccCommonException(ErrorCodeEnum.LOGIN_COUNT_LIMIT_ERROR);
        } catch (AuthenticationException e) {
            log.info("", e);
            throw new PccCommonException(ErrorCodeEnum.SYS_ERROR.getErrorCode(), e.getMessage());
        }
        log.info("登录成功");
        return BaseResult.create(null, true, ErrorCodeEnum.SUCCESS.getErrorCode(), ErrorCodeEnum.SUCCESS.getErrorMsg());
    }



    @SysLog(operation = "添加新用户")
    @ApiOperation(value = "添加新用户")
    @ApiImplicitParam(name = "sysUserForm", value = "新用户数据", type = "body", dataType = "SysUserForm")
    @PostMapping(value = "/newUser")
    public BaseResult<Boolean> addNewUser(@RequestBody SysUserForm sysUserForm) {

        ValidationUtils.validateEntity(sysUserForm);

        SysUserBO sysUserBO = new SysUserBO();
        BeanUtils.copyProperties(sysUserForm, sysUserBO);

        Boolean result = userService.addNewUser(sysUserBO);

        //参数加密， 防止SysLog记录是暴露密码明文
        BeanUtils.copyProperties(sysUserBO, sysUserForm);

        return BaseResult.create(null, result, ErrorCodeEnum.SUCCESS.getErrorCode(), ErrorCodeEnum.SUCCESS.getErrorMsg());
    }


    @ApiOperation(value = "用户登出")
    @PostMapping(value = "/logout")
    public BaseResult<Boolean> logout(){
        ShiroUtils.logout();
        return BaseResult.create(null, true, ErrorCodeEnum.LOGOUT_SUCCESS.getErrorCode(), ErrorCodeEnum.LOGOUT_SUCCESS.getErrorMsg());
    }




}
